﻿
package kliment.display.filters.color {
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.filters.ColorMatrixFilter;
	
	import kliment.display.filters.ImageFilter;

	/**
	* Manipulator for color transformation
	* 
	* @author Kliment
	*/
	public class ColorManipulator extends EventDispatcher {
		private var _displayObject:DisplayObject;
		private var _red:Number = 0;
		private var _green:Number = 0;
		private var _blue:Number = 0;
		private var _alpha:Number = 0;
		private var _saturation:Number = 0;
		private var _hue:Number = 0;
		private var _brightnes:Number = 0;
		private var _contrast:Number = 0;
		private var _colorMatrix:ColorMatrix;
		
		private var _host:ImageFilter;
		
		/**
		 * Constructor
		 * 
		 * @param	displayObject Target Bitmap
		 * @param	_host Parent ImageFilter class
		 */
		public function ColorManipulator(displayObject:DisplayObject, host:ImageFilter) {
			this._host = host;
			_displayObject = displayObject;
		}
		
		/**
		 * @param red, green, blue, alpha = -1..1
		 * @param saturation, brightnes, contrast = -100..100
		 * @param hue = -180..180
		 */
		public function set red(value:Number):void {
			_red = value;
			updateColors();
		}
		
		public function get red():Number {
			return _red;
		}
		
		public function set green(value:Number):void {
			_green = value;
			updateColors();
		}
		
		public function get green():Number {
			return _green;
		}
		
		public function set blue(value:Number):void {
			_blue =  value;
			updateColors();
		}
		
		public function get blue():Number {
			return _blue;
		}
		
		public function set alpha(value:Number):void {
			_alpha =  value;
			updateColors();
		}
		
		public function get alpha():Number {
			return _alpha;
		}
		
		public function set saturation(value:Number):void {
			_saturation = value;
			updateColors();
		}
		
		public function get saturation():Number {
			return _saturation;
		}
		
		public function set hue(value:Number):void {
			_hue = value;
			updateColors();
		}
		
		public function get hue():Number {
			return _hue;
		}
		
		public function set brightnes(value:Number):void {
			_brightnes = value;
			updateColors();
		}
		
		public function get brightnes():Number {
			return _brightnes
		}
		
		public function set contrast(value:Number):void {
			_contrast = value;
			updateColors();
		}
		
		public function get contrast():Number {
			return _contrast;
		}
		
		public function get colorMatrix():Array {
			if (!_colorMatrix)
				_colorMatrix = new ColorMatrix();
			return _colorMatrix;
		}
		
		private function updateColors():void {
			dispatchEvent(new Event(Event.COMPLETE));
			_colorMatrix = new ColorMatrix();
			_colorMatrix.red(_red);
			_colorMatrix.green(_green);
			_colorMatrix.blue(_blue);
			_colorMatrix.alpha(_alpha);
			_colorMatrix.saturation(_saturation);
			_colorMatrix.hue(_hue);
			_colorMatrix.contrast(_contrast);
			_colorMatrix.brightness(_brightnes);
			
			var filters:Array = [new ColorMatrixFilter(_colorMatrix)];
			filters = filters.concat(_host.noiseMatrix);
			
			_displayObject.filters = filters;
			
			dispatchEvent(new Event(Event.COMPLETE));
		}
	}
}
